package myapp.mvc_pattern;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;

public class IO_Files {
	private String FILENAME = "MySampleFile";
	private String FILENAME2 = "MySampleFile2";
	private String FILEPATH = "MyFileStorage";
	private String TAG = "rec1234";
	private File myInternalFile;
	private File myInternalPersonFile;
	
	public IO_Files (Context context) {
		
		ContextWrapper contextWrapper = new ContextWrapper(context);
		File directory = contextWrapper.getDir(FILEPATH, Context.MODE_PRIVATE);
		myInternalFile = new File(directory ,FILENAME);
		myInternalPersonFile = new File(directory, FILENAME2);
	}
	
	public File getFile () {
		return myInternalFile;
	}
	
	public File getPersonFile() {
		return myInternalPersonFile;
	}
	
	public void saveList (List<Item> list) {
		try {
			FileOutputStream fos = new FileOutputStream(myInternalFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (Item i : list) {
				oos.writeObject(i);
			}
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	} 
	
	public List<Item> readList () {
		List<Item> list = new ArrayList<Item>();
		try {
			FileInputStream fis = new FileInputStream(myInternalFile);
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    Object obj = null;
			while ((obj = ois.readObject()) != null) {
				if (obj instanceof Item) {
					list.add((Item) obj);
				}	
			}
			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			Log.d(TAG,"File nicht da");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d(TAG,"IO Bla Keks");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public void savePersonList (List<Person> list) {
		try {
			FileOutputStream fos = new FileOutputStream(myInternalPersonFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (Person p : list) {
				oos.writeObject(p);
			}
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	} 
	
	public List<Person> readPersonList () {
		List<Person> list = new ArrayList<Person>();
		try {
			FileInputStream fis = new FileInputStream(myInternalPersonFile);
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    Object obj = null;
			while ((obj = ois.readObject()) != null) {
				if (obj instanceof Person) {
					list.add((Person) obj);
				}	
			}
			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			Log.d(TAG,"File nicht da");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d(TAG,"IO Bla Keks");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	

	
	public Person findPerson (Person p) {
		Person person = new Person("");
		List<Person> list = readPersonList();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getName().equals(p.getName())) {
				person = p;
			}
		}
		return person;
	}
	
	public void savePersonList2 (Person pers) {
		List<Person> list = readPersonList();
		try {
			FileOutputStream fos = new FileOutputStream(myInternalPersonFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getName().equals(pers.getName())) {
					list.set(i, pers);
				}
				oos.writeObject(list.get(i));
				Log.d(TAG,list.get(i).toString());
			}
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	} 
}
